package com.letian.record.service.impl;

import java.util.*;

import com.letian.common.utils.DateUtils;
import com.letian.record.domain.RecordGenealogyMemberVo;
import com.letian.record.mapper.RecordGenealogyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.letian.record.mapper.RecordGenealogyMemberMapper;
import com.letian.record.domain.RecordGenealogyMember;
import com.letian.record.service.IRecordGenealogyMemberService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 族谱成员Service业务层处理
 * 
 * @author bmz
 * @date 2025-07-20
 */
@Service
public class RecordGenealogyMemberServiceImpl implements IRecordGenealogyMemberService 
{
    @Autowired
    private RecordGenealogyMemberMapper recordGenealogyMemberMapper;

    @Autowired
    private RecordGenealogyMapper recordGenealogyMapper;

    /**
     * 获取族谱成员树形结构
     * @param genealogyId 所属族谱ID
     * @return 树形结构列表
     */
    public List<RecordGenealogyMemberVo> getMemberTreeByGenealogyId(Long genealogyId) {
        RecordGenealogyMember query = new RecordGenealogyMember();
        query.setGenealogyId(genealogyId);
        query.setDelFlag("0");
        // 1. 查询所有成员
        List<RecordGenealogyMember> allMembers = recordGenealogyMemberMapper.selectRecordGenealogyMemberList(query);

        if (allMembers.isEmpty()) {
            return Collections.emptyList();
        }
        ArrayList<RecordGenealogyMemberVo> allMemberVo = new ArrayList<>();
        // 2. 构建 ID -> 成员的映射
        Map<Long, RecordGenealogyMemberVo> memberMap = new HashMap<>();
        for (RecordGenealogyMember item : allMembers) {
            RecordGenealogyMemberVo member = new RecordGenealogyMemberVo();
            member.setId(item.getGenealogyMemberId());
            member.setName(item.getSurname()+item.getMemberName());
            member.setRelation(item.getRelation());
            member.setIsLife(item.getIsAlive() == 1);
            member.setSpouseId(item.getSpouseId());
            member.setParentId(item.getParentId());
            member.setGender(item.getGender());
            member.setHeadPortrait(item.getHeadPortrait());
            memberMap.put(member.getId(), member);
            allMemberVo.add(member);
        }

        // 3. 构建配偶关系
        for (RecordGenealogyMemberVo member : allMemberVo) {
            if (member.getSpouseId() != null && memberMap.containsKey(member.getSpouseId())) {
                if(memberMap.get(member.getSpouseId()).getSpouse() == null)
                {
                    memberMap.get(member.getSpouseId()).setSpouse(new ArrayList<>());
                }
                memberMap.get(member.getSpouseId()).getSpouse().add(member);
            }
        }

        // 4. 构建父子关系树
        List<RecordGenealogyMemberVo> rootNodes = new ArrayList<>();
        for (RecordGenealogyMemberVo member : allMemberVo) {
            if (member.getParentId() == null || !memberMap.containsKey(member.getParentId())) {
                if(member.getSpouseId() != null && memberMap.containsKey(member.getSpouseId())){
                    continue;
                }
                rootNodes.add(member);
            } else {
                RecordGenealogyMemberVo parent = memberMap.get(member.getParentId());
                if(parent.getChildren() == null)
                {
                    parent.setChildren(new ArrayList<>());
                }
                parent.getChildren().add(member);
            }
        }

        return rootNodes;
    }

    /**
     * 查询族谱成员
     * 
     * @param genealogyMemberId 族谱成员主键
     * @return 族谱成员
     */
    @Override
    public RecordGenealogyMember selectRecordGenealogyMemberByGenealogyMemberId(Long genealogyMemberId)
    {
        return recordGenealogyMemberMapper.selectRecordGenealogyMemberByGenealogyMemberId(genealogyMemberId);
    }

    /**
     * 查询族谱成员列表
     * 
     * @param recordGenealogyMember 族谱成员
     * @return 族谱成员
     */
    @Override
    public List<RecordGenealogyMember> selectRecordGenealogyMemberList(RecordGenealogyMember recordGenealogyMember)
    {
        return recordGenealogyMemberMapper.selectRecordGenealogyMemberList(recordGenealogyMember);
    }

    /**
     * 新增族谱成员
     * 
     * @param recordGenealogyMember 族谱成员
     * @return 结果
     */
    @Override
    public int insertRecordGenealogyMember(RecordGenealogyMember recordGenealogyMember)
    {
        recordGenealogyMember.setCreateTime(DateUtils.getNowDate());
        int result = recordGenealogyMemberMapper.insertRecordGenealogyMember(recordGenealogyMember);
        recordGenealogyMapper.updateRecordGenealogyCount(recordGenealogyMember.getGenealogyId());
        return result;
    }

    /**
     * 修改族谱成员
     * 
     * @param recordGenealogyMember 族谱成员
     * @return 结果
     */
    @Override
    public int updateRecordGenealogyMember(RecordGenealogyMember recordGenealogyMember)
    {
        recordGenealogyMember.setUpdateTime(DateUtils.getNowDate());
        return recordGenealogyMemberMapper.updateRecordGenealogyMember(recordGenealogyMember);
    }

    /**
     * 批量删除族谱成员
     * 
     * @param genealogyMemberIds 需要删除的族谱成员主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteRecordGenealogyMemberByGenealogyMemberIds(Long[] genealogyMemberIds)
    {
        RecordGenealogyMember data = recordGenealogyMemberMapper.selectRecordGenealogyMemberByGenealogyMemberId(genealogyMemberIds[0]);
        int result = recordGenealogyMemberMapper.deleteRecordGenealogyMemberByGenealogyMemberIds(genealogyMemberIds);
        recordGenealogyMemberMapper.deleteRecordGenealogyMemberBySpouseId(genealogyMemberIds[0]);
        recordGenealogyMapper.updateRecordGenealogyCount(data.getGenealogyId());
        return result;
    }

    /**
     * 删除族谱成员信息
     * 
     * @param genealogyMemberId 族谱成员主键
     * @return 结果
     */
    @Override
    public int deleteRecordGenealogyMemberByGenealogyMemberId(Long genealogyMemberId)
    {
        RecordGenealogyMember data = recordGenealogyMemberMapper.selectRecordGenealogyMemberByGenealogyMemberId(genealogyMemberId);
        int result = recordGenealogyMemberMapper.deleteRecordGenealogyMemberByGenealogyMemberId(genealogyMemberId);
        recordGenealogyMapper.updateRecordGenealogyCount(data.getGenealogyId());
        return result;
    }
}
